package com.pug.config.mq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Author: lqm
 * create: 2022/5/1 16:56
 * Lover: lqx
 *
 * @see
 */
@Configuration
public class UserChangeOrderTopic {
    public static final String LIU_ORDER_USER_EXCHANGE="liu.order.user.exchange_dead_a";
    public static final String LIU_ORDER_USER_ROUTING_KEY="liu.order.user.routing.key_a";
    public static final String LIU_ORDER_USER_QUEUE="liu.order.user.queue_a";
    public static final String LIU_ORDER_USER_EXCHANGE_DEAD="liu.order.user.exchange_dead_a_dead";
    public static final String LIU_ORDER_USER_ROUTING_KEY_DEAD="liu.order.user.routing.key_a_dead";
    public static final String LIU_ORDER_USER_QUEUE_DEAD="liu.order.user.queue_a_dead";
    //创建普通交换机
    @Bean
    public Exchange UserExchange(){
        return new DirectExchange(LIU_ORDER_USER_EXCHANGE,true,false);
    }
    //创建普通队列
    @Bean
    public Queue UserQueue(){
        return new Queue(LIU_ORDER_USER_QUEUE,true,false,false);
    }
    //创建死信队列
    @Bean
    public Queue createTopicOrderDeadQueue(){
        // 使用Map存放死信队列的三个核心组成部分
        Map<String, Object> args = new HashMap();
        // 创建死信队列交换机
        args.put("x-dead-letter-exchange", LIU_ORDER_USER_EXCHANGE_DEAD);
        // 创建死信队列路由
        args.put("x-dead-letter-routing-key", LIU_ORDER_USER_ROUTING_KEY_DEAD);
        // 设定TTL，单位是ms，下面的单位是10s
        args.put("x-message-ttl", 120000);
        // 创建队列并返回死信队列实例
        return QueueBuilder.durable(LIU_ORDER_USER_QUEUE_DEAD).withArguments(args).build();
    }
    //创建死信交换机
    @Bean
    public Exchange UserOrderExchangeDead(){
        return new DirectExchange(LIU_ORDER_USER_EXCHANGE_DEAD,true,false);
    }
    //将死信队列和扑通交换机绑定
    @Bean
    public Binding a(){
        return BindingBuilder.bind(createTopicOrderDeadQueue()).to(UserExchange()).with(LIU_ORDER_USER_ROUTING_KEY).noargs();
    }
    //将普通队列和死信交换机绑定
    @Bean
    public Binding b(){
        return BindingBuilder.bind(UserQueue()).to(UserOrderExchangeDead()).with(LIU_ORDER_USER_ROUTING_KEY_DEAD).noargs();
    }
}
